package com.inet.baidu;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;




import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.inet.request.ServerRequest;
import com.inet.util.HttpUtil;
import com.inet.util.L;
import com.inet.util.LocusBean;
import com.inet.util.Utils;

/**
 * 百度SDK 监控类
 * 
 * @author YeWen.Xu
 */
public class MyLocationListener implements BDLocationListener {

	public static double lon = 0, lat = 0;
	private static ExecutorService cachedThreadPool = Executors
			.newCachedThreadPool();
	public static boolean isOpenCrawal = false;
	public static double crawlCenterLon = 0.0, crawlCenterLat = 0.0;
	public static int radius = 500;
	public static float speed = 0;
	private static Context mContext;
	public MyLocationListener(Context context) {
		mContext=context;
	}

	/**
	 * 重写父类onReceiveLocation 百度地图地位的回调
	 */
	@Override
	public void onReceiveLocation(BDLocation location) {
		int errorCode = location.getLocType();
		if (errorCode == 61||errorCode==161) {
			onCode_61(location);
		}else {
			L.i("location  code = " + errorCode);
			L.i("Baidu SDK Location Error..............");
		}
	}

	/**
	 * 百度SDK ErrorCode:61 GPS定位成功的回调
	 * 
	 * @param location
	 */
	private void onCode_61(BDLocation location) {
		double tempLat = location.getLatitude();
		double tempLon = location.getLongitude();
		//L.i("lat --lon==(" + tempLat + "," + tempLon + ")");
		this.lat = tempLat;
		this.lon = tempLon;

		// 验证上传必要
		if (!valUpload(location, tempLat, tempLon)) {
			return;
		}
	
		// ACCOFF状态下应该也要监控围栏状态，防止拖车等业务场景
		if (isOpenCrawal) {
			crawalWarn(mContext);
		}
		//把需要上传的点，转成json
		JSONObject json=new JSONObject();
		try {
			json.put("lat", this.lat);
			json.put("lon", this.lon);
			json.put("time", Utils.format.format(new Date()));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		//需要上传的轨迹点， 传到BaiduService的list里面，每隔半个小时上传一次
		if(json!=null)
		BaiduService.list.add(json);
	}

	/**
	 * 验证是否有需要批量上传本次收集的点
	 * 
	 * @param tempLat
	 * @param tempLon
	 * @return
	 */
	private boolean valUpload(BDLocation location, double tempLat,
			double tempLon) {
		this.speed = location.getSpeed();
		
		//通过移动速度来判断是否需要上传
		boolean valMove = this.speed == 0 ? true : false;
		boolean valPoint = (Math.abs(tempLat - this.lat) < 0.0002 && Math
				.abs(tempLon - this.lon) < 0.0002) ? true : false;
		return !valMove && !valPoint;
	}

	/**
	 * 离开围栏区域
	 */
	private void crawalWarn(final Context context) {
		int distance = (int) DistanceUtil
				.getDistance(new LatLng(crawlCenterLat, crawlCenterLon),
						new LatLng(this.lat, this.lon));
		L.i("distance = " + distance);
		if (distance > radius) {
			cachedThreadPool.execute(new Runnable() {
				@Override
				public void run() {
					// 开启围栏警告
					Map<String, String> maps=new HashMap<String, String>();
					maps.put("imei", Utils.getImei(context));
					HttpUtil.sendPost(maps, Utils.getPropertiesValue("post_crawlWarnin"));
				}
			});
		}
	}

	/**
	 * 百度SDK ErrorCode:161 网络定位的成功回调
	 * 
	 * @param location
	 */
	private void onCode_161(BDLocation location) {
		if (this.lat == 0.0 && this.lon == 0.0) {
			this.lat = location.getLatitude();
			this.lon = location.getLongitude();
		}
	}

}
